package com.lemon.smartsafety.ui.adapter.address;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.entity.address.AddrBookUserVo;
import com.lemon.smartsafety.entity.event.address.ContactDeleteEvent;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class AddressContactAdapter extends LemonRcvAdapter<AddrBookUserVo> {

    private LoggerHelper loggerHelper = new LoggerHelper(AddressContactAdapter.class);



    private RecyclerTClickListener<AddrBookUserVo> mListener;

    public AddressContactAdapter(@Nullable List<AddrBookUserVo> data) {
        super(data);
    }

    public AddressContactAdapter(List<AddrBookUserVo> data, RecyclerTClickListener<AddrBookUserVo> listener) {
        super(data);
        this.mListener = listener;
    }


    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new AddressContactItemMenu();
    }

    public class AddressContactItemMenu implements AdapterItem<AddrBookUserVo> {


        @Bind(R.id.tv_tip)
        TextView   tvTip;
        @Bind(R.id.tv_title)
        TextView   tvTitle;
        @Bind(R.id.tv_sub_title)
        TextView   tvSubTitle;
        @Bind(R.id.tv_status_tip)
        TextView   tvStatusTip;
        @Bind(R.id.ripple)
        LinearLayout ripple;
        @Bind(R.id.txtDelete)
        TextView   txtDelete;
        private AddrBookUserVo mData;
        private int            mPosition;


        @Override
        public int getLayoutResId() {
            return R.layout.item_address_book_contact_and_menu;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            txtDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ContactDeleteEvent event = new ContactDeleteEvent();
                    event.setTag(ContactDeleteEvent.class.getName());
                    event.setData(mData);
                    AppService.getBus().post(event);
                }
            });

            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });
        /*    ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });*/
        }

        @Override
        public void handleData(AddrBookUserVo data, int position) {
            mData = data;
            mPosition = position;

            loggerHelper.json(data);


            tvTip.setText(data.getIndexCharI());
            tvTitle.setText(data.getTitleI());
            tvSubTitle.setText(data.getSubTitleI());

            /*控制色块 */
            if (data.getResponse_status() == 0) {
                tvStatusTip.setBackgroundColor(tvStatusTip.getContext().getResources().getColor(R.color.colorDisable));
            } else if (data.getResponse_status() == 1) {
                tvStatusTip.setBackgroundColor(tvStatusTip.getContext().getResources().getColor(R.color.colorAppGreen));
            } else if (data.getResponse_status() == 2) {
                tvStatusTip.setBackgroundColor(tvStatusTip.getContext().getResources().getColor(R.color.colorAppRed));
            }

            if (StringUtils.getBoolean(data.getIs_admin() + "")) {
                tvStatusTip.setBackgroundColor(tvStatusTip.getContext().getResources().getColor(R.color.colorPrimary));
            }


            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {
                tvTip.setVisibility(View.VISIBLE);
            } else {
                tvTip.setVisibility(View.GONE);
            }


        }

    }


    public int getSectionForPosition(int position) {
        return mDataList.get(position).getIndexCharI().charAt(0);
    }

    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr   = mDataList.get(i).getIndexCharI();
            char   firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }
}
